//
//  PickNum11s5ListViewController.m
//  PlayBall
//
//  Created by Huawen Liu on 12-6-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddPickNum11s5ListViewController.h"
#import "JddPickNum11s5ViewController.h"
#import "JddSelectFiveListItemCell.h"
#import "JddViewControllerManager.h"
#import "JddS5F11Number.h"
#import "JddUserProfile.h"
#import "JddCommonFunction.h"

@interface JddPickNum11s5ListViewController ()

@end

@implementation JddPickNum11s5ListViewController

@synthesize numberListTable;
@synthesize multipleTextField;
@synthesize numberArray;
@synthesize noteAmountLabel;
@synthesize multipleLabel;
@synthesize playType;
@synthesize moneyAmountLabel;
@synthesize PlayMethod;
@synthesize PlayMethodTemp;
@synthesize buybutton;
@synthesize bgScrollView;
@synthesize currentLotteryIssue;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddPickNum11s5ListViewController);

- (void)SelectFive2ListItemCellDeleteButtonClicked:(NSNumber *)rowNumber
{

}
- (void)SelectFive3ListItemCellDeleteButtonClicked:(NSNumber *)rowNumber
{
    
}
-(void)destoryView{
    numberArray = [[NSMutableArray alloc] init]; 
    numberArrayTemp = [[NSMutableArray alloc] init];
    [multipleTextField resignFirstResponder];
     multipleTextField.text = @"1";
//    numberArrayTemp = [[NSMutableArray alloc] init];
//    numberArray = [[NSMutableArray alloc] init];
//    [numberArray removeAllObjects];
//    [numberArrayTemp removeAllObjects];
    if (PlayMethod !=PlayMethodTemp) {
        //数据传输
        [numberArray addObjectsFromArray:numberArrayTemp];
        PlayMethod = PlayMethodTemp;
        switch (PlayMethodTemp) {
            case 7001:
                playType.text = @"任选一";
                break;
            case 7002:
                playType.text = @"任选二";
                break;
            case 7003:
                playType.text = @"任选三";
                break;
            case 7004:
                playType.text = @"任选四";
                break;
            case 7005:
                playType.text = @"任选五";
                break;
            case 7006:
                playType.text = @"任选六";
                break;
            case 7007:
                playType.text = @"任选七";
                break;
            case 7008:
                playType.text = @"任选八";
                break;
            case 7009:
                playType.text = @"直选二";
                break;
            case 7010:
                playType.text = @"直选三";
                break;
            case 7011:
                playType.text = @"组选二";
                break;
            case 7012:
                playType.text = @"组选三";
                break;
            default:
                break;
        }
    }
    [numberListTable reloadData];
    noteAmount = 0;
    for (JddS5F11Number *dltNumber in numberArray) {
        if (PlayMethod==7011) {
            if ([dltNumber.BallArray count]!=2) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }   
        }else if (PlayMethod== 7012){
            if ([dltNumber.BallArray count]!=3) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }
        }else if (PlayMethod== 7009){
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        noteAmount++;
                    }
                }
            }
            
        }else if (PlayMethod== 7010){
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                            if (number3!=number2&&number3!=number) {
                                noteAmount++;
                            }
                        }
                    }
                }
            }
        }else if(PlayMethod<7009){
            if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }
        }
        
    }
    noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
    
    discountNo = [NSMutableString string];
    discountAmount = 0;
}

- (IBAction)coBuyBtnClick:(id)sender {
    if([self checkValue]){
    JddLotteryIssue *ls = ((JddPickNum11s5ViewController *)self.fromController).currentLotteryIssue;
    [FaQiHeMaiViewController sharedInstance].bs = [multipleTextField.text intValue];
    [FaQiHeMaiViewController sharedInstance].lotID = LOTTERY_TYPE_ID_11S5;
    [FaQiHeMaiViewController sharedInstance].issueID = ls.code;
    [FaQiHeMaiViewController sharedInstance].issueName = ls.name;
    [FaQiHeMaiViewController sharedInstance].issueNameTitle = [NSString stringWithFormat:@"11选5第%@期合买",ls.name];
    [FaQiHeMaiViewController sharedInstance].allMoney = noteAmount * [multipleTextField.text intValue] * 2;
    [FaQiHeMaiViewController sharedInstance].lotteryNumber = [self lotteryNumberList];
    [[JddViewControllerManager sharedInstance].navigationController pushViewController:[FaQiHeMaiViewController sharedInstance] animated:YES];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"]invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    
    BOOL canChange = [string isEqualToString:filtered];
    
    if (!canChange) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数只能输入数字"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    if ([string length] + range.location>3) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    
    if ([textField.text length]== 0) {
        if ([string isEqualToString:@"0"]) {
            return NO;
        }else {
            moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
            return YES;
        }
        
    }
    if (range.location >=3) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于999"
                                                       delegate:nil 
                                              cancelButtonTitle:@"确定" 
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }else {
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
        return YES;
    }
    
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}// clear button事件,返回NO过滤之
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}// 'return' key事件.返回NO过滤之

- (IBAction)clearListButtonClicked:(id)sender{
    if ([numberListTable numberOfRowsInSection:0]>0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"确认清空列表？"
                                                       delegate:self 
                                              cancelButtonTitle:@"确定" 
                                              otherButtonTitles:@"取消",nil ];
        [alert show];

    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    
    [self.bgScrollView setContentOffset:CGPointMake(0,213
                                                    /*buybutton.frame.size.height + buybutton.frame.origin.y - multipleTextField.frame.origin.y + multipleTextField.frame.size.height*2+20*/)
                               animated:YES];
    
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:buybutton]) {
        return NO;
    }
    
    return YES;
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    [multipleTextField resignFirstResponder];
    if ([multipleTextField.text length]==0) {
        multipleTextField.text = @"1";
    }
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
    
    discountNo = [NSMutableString string];
    discountAmount = 0;
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void) alertView:(UIAlertView *)alertview
clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([[alertview message] isEqualToString:@"余额不足，请充值！"]) {
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_SELECT_RECHARGE_WAY 
                                                                 fromController:self 
                                                                     withObject:nil 
                                                                    withObject2:nil];

        }
    }else if ([[alertview message] isEqualToString:@"投注成功"]) {
        if (buttonIndex==0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_HALL 
                                                                 fromController:self 
                                                                     withObject:nil 
                                                                    withObject2:nil];
            
        }
        if (buttonIndex ==1) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_OPEN 
                                                                 fromController:self 
                                                                     withObject:nil 
                                                                    withObject2:nil];
        }
    } else if([[alertview message] isEqualToString:lotString]){
        if (buttonIndex==0) {
            [self sureToBuy];
        }        
    } else if ([[alertview message] isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN 
                                                                 fromController:self 
                                                                     withObject:nil 
                                                                    withObject2:nil];
        }
    } else if ([[alertview message] isEqualToString:@"确认清空列表？"]){
        if(buttonIndex == 0){
        [numberArrayTemp removeAllObjects];
        [numberArray removeAllObjects];
        if (PlayMethod !=PlayMethodTemp) {
            //数据传输
            [numberArray addObjectsFromArray:numberArrayTemp];
            PlayMethod = PlayMethodTemp;
            switch (PlayMethodTemp) {
                case 7001:
                    playType.text = @"任选一";
                    break;
                case 7002:
                    playType.text = @"任选二";
                    break;
                case 7003:
                    playType.text = @"任选三";
                    break;
                case 7004:
                    playType.text = @"任选四";
                    break;
                case 7005:
                    playType.text = @"任选五";
                    break;
                case 7006:
                    playType.text = @"任选六";
                    break;
                case 7007:
                    playType.text = @"任选七";
                    break;
                case 7008:
                    playType.text = @"任选八";
                    break;
                case 7009:
                    playType.text = @"直选二";
                    break;
                case 7010:
                    playType.text = @"直选三";
                    break;
                case 7011:
                    playType.text = @"组选二";
                    break;
                case 7012:
                    playType.text = @"组选三";
                    break;
                default:
                    break;
            }
        }
        [numberListTable reloadData];
        noteAmount = 0;
        for (JddS5F11Number *dltNumber in numberArray) {
            if (PlayMethod==7011) {
                if ([dltNumber.BallArray count]!=2) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }   
            }else if (PlayMethod== 7012){
                if ([dltNumber.BallArray count]!=3) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }else if (PlayMethod== 7009){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            noteAmount++;
                        }
                    }
                }
                
            }else if (PlayMethod== 7010){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                                if (number3!=number2&&number3!=number) {
                                    noteAmount++;
                                }
                            }
                        }
                    }
                }
            }else if(PlayMethod<7009){
                if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }
            
        }
        multipleTextField.text = @"1";
        noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
        multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
        
        discountNo = [NSMutableString string];
        discountAmount = 0;
        }
    } else if ([[alertview message] isEqualToString:@"一个方案只能有一种玩法，如果继续将清空以前所选号码，确认清空列表？"] ) {
    if (buttonIndex==0) {
        [numberArray removeAllObjects];
        if (PlayMethod !=PlayMethodTemp) {
            //数据传输
            [numberArray addObjectsFromArray:numberArrayTemp];
            PlayMethod = PlayMethodTemp;
            switch (PlayMethodTemp) {
                case 7001:
                    playType.text = @"任选一";
                    break;
                case 7002:
                    playType.text = @"任选二";
                    break;
                case 7003:
                    playType.text = @"任选三";
                    break;
                case 7004:
                    playType.text = @"任选四";
                    break;
                case 7005:
                    playType.text = @"任选五";
                    break;
                case 7006:
                    playType.text = @"任选六";
                    break;
                case 7007:
                    playType.text = @"任选七";
                    break;
                case 7008:
                    playType.text = @"任选八";
                    break;
                case 7009:
                    playType.text = @"直选二";
                    break;
                case 7010:
                    playType.text = @"直选三";
                    break;
                case 7011:
                    playType.text = @"组选二";
                    break;
                case 7012:
                    playType.text = @"组选三";
                    break;
                default:
                    break;
            }
        }
        [numberListTable reloadData];
        noteAmount = 0;
        for (JddS5F11Number *dltNumber in numberArray) {
            if (PlayMethod==7011) {
                if ([dltNumber.BallArray count]!=2) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }   
            }else if (PlayMethod== 7012){
                if ([dltNumber.BallArray count]!=3) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }else if (PlayMethod== 7009){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            noteAmount++;
                        }
                    }
                }
                
            }else if (PlayMethod== 7010){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                                if (number3!=number2&&number3!=number) {
                                    noteAmount++;
                                }
                            }
                        }
                    }
                }
            }else if(PlayMethod<7009){
                if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }
            
        }
        noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
        multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
        
        discountNo = [NSMutableString string];
        discountAmount = 0;
    }else if (buttonIndex==1) {
        numberArrayTemp = [[NSMutableArray alloc] init];
    }

    }
    
}

- (IBAction)plusButtonClicked:(id)sender{
    NSInteger multiple = [multipleTextField.text intValue];
    if (multiple>998) {
        return;
    }
    multipleTextField.text = [NSString stringWithFormat:@"%d", ++multiple];
    multipleLabel.text =[NSString stringWithFormat:@"%d倍",[multipleTextField.text intValue]];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
}

- (IBAction)minusButtonClicked:(id)sender{
    NSInteger multiple = [multipleTextField.text intValue];
    if (multiple-- < 2) {
        multipleTextField.text = @"1";
        return;
    }
    
    multipleTextField.text = [NSString stringWithFormat:@"%d", multiple];
    multipleLabel.text =[NSString stringWithFormat:@"%d倍",[multipleTextField.text intValue]];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
}

- (NSMutableArray*)lotteryNumberList{
    NSMutableArray *paramNumberArray = [[NSMutableArray alloc] init];
    for (JddS5F11Number *dltNumber in numberArray) {
        NSMutableString *lotteryNumberString = [NSMutableString string];
        NSMutableDictionary *numDic = [[NSMutableDictionary alloc] init];
        [numDic setObject:[NSNumber numberWithInt:PlayMethod] forKey:@"playid"];
        if (PlayMethod == 7009) {
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        [lotteryNumberString appendFormat:@"%02d", [number intValue]];
                        [lotteryNumberString appendString:@"|"];
                        [lotteryNumberString appendFormat:@"%02d", [number2 intValue]];
                        [lotteryNumberString appendString:@"&"];
                    }
                }
            }

        } else if (PlayMethod == 7010) {
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                            if (number3!=number2&&number3!=number) {
                                [lotteryNumberString appendFormat:@"%02d", [number intValue]];
                                [lotteryNumberString appendString:@"|"];
                                [lotteryNumberString appendFormat:@"%02d", [number2 intValue]];
                                [lotteryNumberString appendString:@"|"];
                                [lotteryNumberString appendFormat:@"%02d", [number3 intValue]];
                                [lotteryNumberString appendString:@"&"];
                            }
                        }
                    }
                }
            }
        }
        else {
            for (NSNumber *number in dltNumber.BallArray) {
                [lotteryNumberString appendFormat:@"%02d ", [number intValue]];
            }
        }
        if(PlayMethod == 7010 || PlayMethod == 7009){
            NSArray *lotteryNumberArray = [[lotteryNumberString substringToIndex:(lotteryNumberString.length - 1)] componentsSeparatedByString:@"&"];
            for(NSString *s in lotteryNumberArray){
                NSMutableDictionary *numDic = [[NSMutableDictionary alloc] init];
                [numDic setObject:[NSNumber numberWithInt:PlayMethod] forKey:@"playid"];
                [numDic setObject:s forKey:@"number"];
                [paramNumberArray addObject:numDic];
            }
        }else{
            [numDic setObject:lotteryNumberString forKey:@"number"];
            [paramNumberArray addObject:numDic];
        }
    }
    return paramNumberArray;
}

- (void)BuyLotteryOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if(result)
    {
        //NSString *resultDataString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        //NSLog(@"resultDataString=%@",resultDataString);
        NSDictionary *ReDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
        if([[ReDic objectForKey:@"Code"] intValue] == 0)
        {
            // modify by rainy  修改投注使用接口不经过web
            NSString * fanganhao = [ReDic objectForKey:@"Data"];
            JddLotteryIssue *ls = ((JddPickNum11s5ViewController *)self.fromController).currentLotteryIssue;
            alert1 = [JddAlertView getInstanceView];
            alert1.lssueString = fanganhao;
            alert1.moneylable = moneyAmountLabel.text;
            [alert1 alertBuyMessage:[NSString stringWithFormat:@"11选5 %@期",ls.name]];
//            NSString*checkUrlString = [NSString stringWithFormat:@"%@?ck=%@",BASE_SERVER_IOS_BUY_LOTTERY_URL,[ReDic objectForKey:@"Data"]];
//            NSURL *checkUrl = [NSURL URLWithString:checkUrlString];
//            [[UIApplication sharedApplication] openURL:checkUrl];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"购彩失败"                                                  message:[ReDic objectForKey:@"Data"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil ];
            [alert show];
        }
//        [JddCommonFunction updateBuyLotteryListWithLotteryId:LOTTERY_TYPE_ID_11S5];
    }
}

-(void)QueryUserInfoOperationResult:(NSData *)result{
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            JddBuyLotteryOperation *operation = [[JddBuyLotteryOperation alloc] init];
            operation.delegate = self;            
            JddLotteryIssue *ls = currentLotteryIssue;
            operation.lotteryTypeId = [NSNumber numberWithInt:70];
            operation.issueId = [NSNumber numberWithInt:[ls.code intValue]];
            operation.multiple =[NSNumber numberWithInt:[multipleTextField.text intValue]];
            operation.moneyAmount = [NSNumber numberWithInt:noteAmount * [multipleTextField.text intValue] * 2];
            operation.lotteryNumber = [self lotteryNumberList];
            [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
            
        }else {
            if ([[JddViewControllerManager sharedInstance].navigationController topViewController] == [JddPickNum11s5ListViewController sharedInstance]) {
                [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            }            
        }
    }
}

//- (void)QueryDiscountOperationResult:(NSData *)result{
//    if (result) {
//        if ([JddDataParser parseDiscountResult:result discountNo:discountNo discountAmount:discountAmount]){
//            QueryUserInfoOperation *operation = [[QueryUserInfoOperation alloc] init];
//            operation.delegate = self;
//            [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
//        }
//        else {
//            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
//            [JddCommonFunction setWaitingViewHidden:YES fromController:self];
//        }
//    }
//    
//}

-(void)sureToBuy{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryUserInfoOperation *operation = [[JddQueryUserInfoOperation alloc] init];
    operation.delegate = self;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    
//    QueryDiscountOperation *operation = [[QueryDiscountOperation alloc] init];
//    operation.delegate = self;
//    operation.lotteryTypeId = [NSString stringWithFormat:@"%d", LOTTERY_TYPE_ID_11S5];
//    operation.castTypeNo = [NSString stringWithFormat:@"%d", 0];
//    operation.moneyAmount = [NSString stringWithFormat:@"%d", noteAmount * [multipleTextField.text intValue] * 2];
//    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    
    
}

-(void)QueryDigitalLotteryIssueOperationResult:(NSData *)result{
    int multiple = [multipleTextField.text intValue];
    if (result) {
        if ([JddDataParser parseLotteryIssueResult:result lotteryIssue:currentLotteryIssue]){
            lotString =[NSString stringWithFormat:@"当前为%@期，确认购买？", currentLotteryIssue.name];
            if (![JddCommonFunction isLogin]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                                message:@"您还没有登录，请先登录"
                                                               delegate:self 
                                                      cancelButtonTitle:@"确定" 
                                                      otherButtonTitles:@"取消",nil ];
                [alert show];
            } else {
                if (!agreeLotteryForFooterView.checkBoxBtn.selected) {
                    [JddCommonFunction messageBoxWithMessage:BUY_NOT_READ];
                }
                else if ([numberArray count]==0) {
                    [JddCommonFunction messageBoxWithMessage:BALL_COUNT_NULL];
                }else{
                    if (multiple == 0) {
                        [JddCommonFunction messageBoxWithMessage:MULTIPLE_Tip];
                    }else{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                                    message:lotString
                                                                   delegate:self 
                                                          cancelButtonTitle:@"确定" 
                                                          otherButtonTitles:@"取消",nil ];
                    [alert show];
                        }
                }
            }

        }
    }
}


- (IBAction)buyButtonClicked:(id)sender{
    if ([self checkValue]) {
    JddQueryDigitalLotteryIssueOperation *operation = [[JddQueryDigitalLotteryIssueOperation alloc] init];
    operation.delegate = self;
    operation.lotteryTypeId = @"70";
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
    
}

-(BOOL)checkValue{
    if ([multipleTextField.text intValue]>999) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        multipleTextField.text =  @"999";
        return NO;
    }else if ((noteAmount * [multipleTextField.text intValue] * 2) > 2000000 ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"单个方案的总金额必须在2彩豆至2000000彩豆之间"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    return YES;
}

- (IBAction)continueSelectNumberButtonClicked:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)RandomSelectNumViewClickedButtonAtIndex:(NSNumber*)buttonIndex multiple:(NSNumber*)multiple{
    if ([buttonIndex intValue]== 1) {        
        for (int i = 0; i < [multiple intValue]; ++i) {
            JddS5F11Number *number = [[JddS5F11Number alloc] init];
            [JddCommonFunction generateBallOf11s5Number:number type:PlayMethod];
            
            [numberArray addObject:number];
        }
        
        [numberListTable reloadData];
        noteAmount = 0;
        for (JddS5F11Number *dltNumber in numberArray) {
            if (PlayMethod==7011) {
                if ([dltNumber.BallArray count]!=2) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }   
            }else if (PlayMethod== 7012){
                if ([dltNumber.BallArray count]!=3) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }else if (PlayMethod== 7009){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                                    noteAmount++;
                        }
                    }
                }
                
            }else if (PlayMethod== 7010){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                                if (number3!=number2&&number3!=number) {
                                    noteAmount++;
                                }
                            }
                        }
                    }
                }
            }else if(PlayMethod<7009){
                if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }
            
        }
        noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
        multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
        
        discountNo = [NSMutableString string];
        discountAmount = 0;
    }    
}

- (IBAction)continueRandomSelectNumberButtonClicked:(id)sender{
//    CGRect rect = [[UIScreen mainScreen] bounds];
//    rect.origin.x = (rect.size.width - 251) / 2.0;
//    rect.origin.y = (rect.size.height - 139) / 2.0;
//    rect.size.width = 251.0;
//    rect.size.height = 139.0;
    
    randomSelectNumberDialog = [JddRandomSelectNumView getInstanceView];
//    [randomSelectNumberDialog setFrame:rect];
    randomSelectNumberDialog.type =1;
    randomSelectNumberDialog.delegate = self;
    [randomSelectNumberDialog okButtonClicked:nil];//yan+
//    [randomSelectNumberDialog show];
}

-(IBAction)textFieldDoneEditing:(id)sender{
    [multipleTextField resignFirstResponder];
}

- (void)numberFromArray:(NSArray*)inNumberArray{
    if (!numberArrayTemp) {
        numberArrayTemp = [NSMutableArray array];
    }
        [numberArrayTemp removeAllObjects];
        [numberArrayTemp addObjectsFromArray:inNumberArray];
    if (PlayMethod==PlayMethodTemp||PlayMethod==0||[numberArray count]==0) {
        [numberArray addObjectsFromArray:numberArrayTemp];
        PlayMethod=PlayMethodTemp;
        switch (PlayMethodTemp) {
            case 7001:
                playType.text = @"任选一";
                break;
            case 7002:
                playType.text = @"任选二";
                break;
            case 7003:
                playType.text = @"任选三";
                break;
            case 7004:
                playType.text = @"任选四";
                break;
            case 7005:
                playType.text = @"任选五";
                break;
            case 7006:
                playType.text = @"任选六";
                break;
            case 7007:
                playType.text = @"任选七";
                break;
            case 7008:
                playType.text = @"任选八";
                break;
            case 7009:
                playType.text = @"直选二";
                break;
            case 7010:
                playType.text = @"直选三";
                break;
            case 7011:
                playType.text = @"组选二";
                break;
            case 7012:
                playType.text = @"组选三";
                break;
            default:
                break;
        }
        [numberListTable reloadData];
        noteAmount = 0;
        for (JddS5F11Number *dltNumber in numberArray) {
            if (PlayMethod==7011) {
                if ([dltNumber.BallArray count]!=2) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }   
            }else if (PlayMethod== 7012){
                if ([dltNumber.BallArray count]!=3) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }else if (PlayMethod== 7009){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            noteAmount++;
                        }
                    }
                }
                
            }else if (PlayMethod== 7010){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                                if (number3!=number2&&number3!=number) {
                                    noteAmount++;
                                }
                            }
                        }
                    }
                }
            }else if(PlayMethod<7009){
                if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }
            
            
        }
        noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
        multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
        
        discountNo = [NSMutableString string];
        discountAmount = 0;
    }
    

    
}

- (void)SelectFiveListItemCellDeleteButtonClicked:(NSNumber *)rowNumber{
    for (int i = [rowNumber intValue] + 1; i < [numberArray count]; ++i) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [numberListTable cellForRowAtIndexPath:indexPath];
        cell.tag--;
    }
    
    [numberArray removeObjectAtIndex:[rowNumber intValue]];
    [numberListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[rowNumber intValue] inSection:0]] withRowAnimation:YES];
    
    noteAmount = 0;
    for (JddS5F11Number *dltNumber in numberArray) {
        if (PlayMethod==7011) {
            if ([dltNumber.BallArray count]!=2) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }   
        }else if (PlayMethod== 7012){
            if ([dltNumber.BallArray count]!=3) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }
        }else if (PlayMethod== 7009){
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        noteAmount++;
                    }
                }
            }
            
        }else if (PlayMethod== 7010){
            for (NSNumber *number in dltNumber.BallArray) {
                for (NSNumber *number2 in dltNumber.SecondBallArray) {
                    if (number2!=number) {
                        for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                            if (number3!=number2&&number3!=number) {
                                noteAmount++;
                            }
                        }
                    }
                }
            }
        }else if(PlayMethod<7009){
            if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
            }
            else {
                noteAmount++;
            }
        }
        
    }
    noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
    multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
    moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
    
    discountNo = [NSMutableString string];
    discountAmount = 0;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DELETE;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [numberArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (PlayMethod == 7009) {
        return 54.0;
    }else if (PlayMethod == 7010) {
        return 60.0;
    }else {
        return 48.0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (PlayMethod==7009) {
        JddSelectFive2ListItemCell *cell = [JddSelectFive2ListItemCell getInstanceView];
        
        JddS5F11Number *dltNumber = [numberArray objectAtIndex:indexPath.row];
        NSMutableString *BallNumberString = [NSMutableString stringWithCapacity:[dltNumber.BallArray count]];
        NSMutableString *SecondBallNumberString = [NSMutableString stringWithCapacity:[dltNumber.SecondBallArray count]];
        for (NSNumber *number in [JddCommonFunction sortArray: dltNumber.BallArray]) {
            [BallNumberString appendFormat:@"%02d ", [number intValue]];
        }
        for (NSNumber *number in [JddCommonFunction sortArray:dltNumber.SecondBallArray]) {
            [SecondBallNumberString appendFormat:@"%02d ", [number intValue]];
        }
        cell.SecondBallLabel.text = SecondBallNumberString;
        cell.BallLabel.text = BallNumberString;    
        cell.PlayM = PlayMethod;
        cell.tag = indexPath.row;
        cell.delegate = self;
        
        return cell;

    } else if (PlayMethod == 7010) {
        JddSelectFive3ListItemCell *cell = [JddSelectFive3ListItemCell getInstanceView];
        
        JddS5F11Number *dltNumber = [numberArray objectAtIndex:indexPath.row];
        NSMutableString *BallNumberString = [NSMutableString stringWithCapacity:[dltNumber.BallArray count]];
        NSMutableString *SecondBallNumberString = [NSMutableString stringWithCapacity:[dltNumber.SecondBallArray count]];
        NSMutableString *ThirdBallNumberString = [NSMutableString stringWithCapacity:[dltNumber.ThirdBallArray count]];
        for (NSNumber *number in [JddCommonFunction sortArray: dltNumber.BallArray]) {
            [BallNumberString appendFormat:@"%02d ", [number intValue]];
        }
        for (NSNumber *number in [JddCommonFunction sortArray: dltNumber.SecondBallArray]) {
            [SecondBallNumberString appendFormat:@"%02d ", [number intValue]];
        }
        for (NSNumber *number in [JddCommonFunction sortArray: dltNumber.ThirdBallArray]) {
            [ThirdBallNumberString appendFormat:@"%02d ", [number intValue]];
        }
        cell.SecondBallLabel.text = SecondBallNumberString;
        cell.ThirdBallLabel.text = ThirdBallNumberString;
        cell.BallLabel.text = BallNumberString;    
        cell.PlayM = PlayMethod;
        cell.tag = indexPath.row;
        cell.delegate = self;
        
        return cell;
    }else {
    JddSelectFiveListItemCell *cell = [JddSelectFiveListItemCell getInstanceView];
    
    JddS5F11Number *dltNumber = [numberArray objectAtIndex:indexPath.row];
    NSMutableString *BallNumberString = [NSMutableString stringWithCapacity:[dltNumber.BallArray count]];
    for (NSNumber *number in [JddCommonFunction sortArray: dltNumber.BallArray]) {
        [BallNumberString appendFormat:@"%02d ", [number intValue]];
    }
    cell.BallLabel.text = BallNumberString;    
    cell.PlayM = PlayMethod;
    cell.tag = indexPath.row;
    cell.delegate = self;
    
    return cell;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    agreeLotteryForFooterView = [JddAgreeLotteryForFooterView getInstanceView];
    agreeLotteryForFooterView.fromViewController = self;
    numberListTable.tableFooterView = agreeLotteryForFooterView;
    multipleTextField.returnKeyType = UIReturnKeyGo;
//    [self.navigationItem setTitle:PAGE_11S5_PICK_NUMBER_LIST];
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_11S5_PICK_NUMBER_LIST];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
//        //self.navigationItem.hidesBackButton = YES;

    if (!numberArray) {
        numberArray = [[NSMutableArray alloc] init];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"11X5-detail"];
    
    if ([multipleTextField.text length]==0) {
        multipleTextField.text = @"1";
    }
    [multipleTextField resignFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"11X5-detail"];

    currentLotteryIssue = [[JddLotteryIssue alloc] init];
    
    buybutton.hidden = NO;
    if (PlayMethod!=PlayMethodTemp&&PlayMethod!=0&&[numberArray count]>0&&[numberArrayTemp count]>0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" 
                                                        message:@"一个方案只能有一种玩法，如果继续将清空以前所选号码，确认清空列表？"
                                                       delegate:self 
                                              cancelButtonTitle:@"确定" 
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    }else {
        switch (PlayMethodTemp) {
            case 7001:
                playType.text = @"任选一";
                break;
            case 7002:
                playType.text = @"任选二";
                break;
            case 7003:
                playType.text = @"任选三";
                break;
            case 7004:
                playType.text = @"任选四";
                break;
            case 7005:
                playType.text = @"任选五";
                break;
            case 7006:
                playType.text = @"任选六";
                break;
            case 7007:
                playType.text = @"任选七";
                break;
            case 7008:
                playType.text = @"任选八";
                break;
            case 7009:
                playType.text = @"直选二";
                break;
            case 7010:
                playType.text = @"直选三";
                break;
            case 7011:
                playType.text = @"组选二";
                break;
            case 7012:
                playType.text = @"组选三";
                break;
            default:
                break;
        }
        noteAmount = 0;
        for (JddS5F11Number *dltNumber in numberArray) {
            if (PlayMethod==7011) {
                if ([dltNumber.BallArray count]!=2) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }   
            }else if (PlayMethod== 7012){
                if ([dltNumber.BallArray count]!=3) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }else if (PlayMethod== 7009){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            noteAmount++;
                        }
                    }
                }
                
            }else if (PlayMethod== 7010){
                for (NSNumber *number in dltNumber.BallArray) {
                    for (NSNumber *number2 in dltNumber.SecondBallArray) {
                        if (number2!=number) {
                            for (NSNumber *number3 in dltNumber.ThirdBallArray) {
                                if (number3!=number2&&number3!=number) {
                                    noteAmount++;
                                }
                            }
                        }
                    }
                }
            }else if(PlayMethod<7009){
                if ([dltNumber.BallArray count]!=(PlayMethod-7000)) {
                    noteAmount += [JddCommonFunction countS5TotalNumber:[dltNumber.BallArray count] playmethod:PlayMethod];
                }
                else {
                    noteAmount++;
                }
            }
            
        }
        noteAmountLabel.text = [NSString stringWithFormat:@"%d注", noteAmount];
        multipleLabel.text = [NSString stringWithFormat:@"%@倍", multipleTextField.text];
        moneyAmountLabel.text = [NSString stringWithFormat:@"%d彩豆", noteAmount * [multipleTextField.text intValue] * 2];
        
        discountNo = [NSMutableString string];
        discountAmount = 0;

    }
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
